<template>
  <div class="login-page">
    <!-- 1.顶部栏 -->
    <van-nav-bar title="面经登录" />
    <!-- 2.注册表单 -->
    <van-form @submit="onSubmit">
      <!-- 2.1用户名 -->
      <van-field
       :rules="[
       { required: true, message: '请填写用户名' },
       {pattern:/^\w{6,15}$/, message: '用户名必须在6-15位之间'}
       ]"
       v-model="formData.username"
       name="username"
        label="用户名"
        placeholder="用户名"></van-field>
        <!-- 2.2密码 -->
      <van-field
       :rules="[
       { required: true, message: '请填写密码' },
       {pattern:/^\w{6,15}$/, message: '密码必须在6-15位之间'}
       ]"
        v-model="formData.password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
      ></van-field>
      <!-- 2.3注册按钮 -->
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
      <router-link class="link" to="/register">还未注册,去注册 </router-link>
    </van-form>
  </div>
</template>

<script>
import { UserLogin } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'login-page',
  data () {
    return {
      formData: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      const data = await UserLogin(values)
      console.log(data)
      setToken(data.data.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
